package com.tc.juc.pack05;

import lombok.extern.slf4j.Slf4j;

/**
 * @Auther: tianchao
 * @Date: 2021/10/4 23:05
 * @Description:
 */
public class Test33TwoPhaseVolitile {

    public static void main(String[] args) throws InterruptedException {
        TwoPhaseTermination twoPhaseTermination = new TwoPhaseTermination();
        twoPhaseTermination.start();
        Thread.sleep(2000);
        twoPhaseTermination.stop();
    }
}
@Slf4j
class TwoPhaseTermination{
    private Thread thread;

    private volatile boolean stop = false;

    private volatile boolean starting = false;

    public void start(){
        synchronized (this){
            if (starting){
                return;
            }else {
                starting = true;
            }
        }

        thread = new Thread(()->{
            while (true){
                Thread current = Thread.currentThread();
                //if (current.isInterrupted()){
                if (stop){
                    log.info("料理后事");
                    starting = false;
                    break;
                }
//                try {
//                    Thread.sleep(1);
//                    log.info("监控");
//                } catch (InterruptedException e) {
//                    e.printStackTrace();
//                    //current.interrupt();
//                }
            }
        },"monitor");
        thread.start();
    }
    public void stop(){
        thread.interrupt();
        stop = true;
    }
    
}

